package com.cf.edu182;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

public class c {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException {
        int T = Integer.parseInt(br.readLine().trim());
        while (T-- > 0) {
            solve();
        }
        out.flush();
        out.close();
        br.close();
    }

    static final long MOD = 998244353L;

    static void solve() throws IOException {
        int n = Integer.parseInt(br.readLine().trim());
        String[] in = br.readLine().split(" ");
        int[] a = new int[n];
        int[] b = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(in[i]);
        }
        in =  br.readLine().split(" ");
        for (int i = 0; i < n; i++) {
            b[i] = Integer.parseInt(in[i]);
        }
        long dp0 = 1, dp1 = 1; // for i = 0 (first position) we can choose 0 or 1

        for (int i = 0; i < n - 1; i++) {
            boolean[][] allow = new boolean[2][2];
            // s = state at i, t = state at i+1
            for (int s = 0; s <= 1; s++) {
                for (int t = 0; t <= 1; t++) {
                    long first_i = (s == 0) ? a[i] : b[i];
                    long first_next = (t == 0) ? a[i+1] : b[i+1];
                    long second_i = (s == 0) ? b[i] : a[i];
                    long second_next = (t == 0) ? b[i+1] : a[i+1];
                    if (first_i <= first_next && second_i <= second_next) {
                        allow[s][t] = true;
                    } else {
                        allow[s][t] = false;
                    }
                }
            }

            long ndp0 = 0, ndp1 = 0;
            if (allow[0][0]) ndp0 = (ndp0 + dp0) % MOD;
            if (allow[1][0]) ndp0 = (ndp0 + dp1) % MOD;
            if (allow[0][1]) ndp1 = (ndp1 + dp0) % MOD;
            if (allow[1][1]) ndp1 = (ndp1 + dp1) % MOD;

            dp0 = ndp0;
            dp1 = ndp1;
        }

        long ans = (dp0 + dp1) % MOD;
        out.println(ans);

    }
}
